$(function(){
	auth_onload.begin();
});

authUtils = {
		key :'',
		getkey : function(){
			$.ajax( {
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/login!getKey",
				success : function(data) {
					if('ok' == data.state){
						var y = $.cookie("y");
						//$.cookie("y",temp);
						//alert("test");
					}
				} 
			});
			/*
			$.ajax( {
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!checkUsername?username=great",
				success : function(data) {
					//alert(data);
				},
				error :  function(a,b,c) {
					var text = a.responseText;
					text = text.substr(0,15);
					var regex=/login/;
					var m = regex.test(text);
					alert(m);
				}
			});
			*/
		},
		login : function(){
			
			var username = $("#username").val();
			username = $.trim(username);
			var pw = $("#passwd").val();
			
			pw = $.trim(pw);
			
			var value = username+" "+pw; 
			var y = $.cookie("y");
			
			var k = y.split(" ")[1];
			var key =  k.replace('"','');
			
			value = authUtils.encrypt(value,key);
			//username = $.trim(username);
			if(''==username || username.length<2){
				alert('用户名不能是空,用户名不能小于2位');
				return;
			}
			
			if(''==pw || pw.length<6){
				alert('密码不能是空,密码长度不能够小于6位');
				return;
			}
			//$.cookie("to",null);
			
			$.ajax( {
				data : 'value='+value,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/login!weblogin",
				success : function(data) {
					if('ok' == data.state){
						var t = data.t;
						$.cookie("to",t,{domain:global_parameter.cookie_domain});
						$.cookie("k",key,{domain:global_parameter.cookie_domain});
						//$.cookie("y",null);
						$("#msg").html("");
						$("#passwd").val("");
						var timestamp = Date.parse(new Date());
						var time = timestamp/1000;
						time = authUtils.encrypt(time,key);
						time = authUtils.stringToHex(time);
						$.cookie("ti",time,{domain:global_parameter.cookie_domain});
						$.cookie("un",username,{domain:global_parameter.cookie_domain});
						//window.location.reload();
						window.location.href=global_parameter.main_page;
						//alert(data.state);
					}else if('timeout'==data.state){
						alert(data.msg);
						window.location.reload();
						$("#msg").html(data.msg);
					}else{
						alert(data.msg);
						$("#msg").html(data.msg);
					}
				} 
			});
		},
		encrypt : function (word,key){
			
	    	 var key = CryptoJS.enc.Utf8.parse(key);    
	         var iv  = CryptoJS.enc.Utf8.parse('0102030405060755');    
	         var srcs = CryptoJS.enc.Utf8.parse(word);   
	         var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC});   
	         return encrypted.toString();   
    	},
    	stringToHex : function (str){
    			var val="";
    			for(var i = 0; i < str.length; i++){
    				if(val == ""){
    					val = str.charCodeAt(i).toString(16);
    				}else{
    					val += str.charCodeAt(i).toString(16);
    				}
    			}
    		return val;
    	}
};

auth_onload = {
		begin : function(){
			authUtils.getkey();
			document.onkeydown = function(e){ 
			var ev = document.all ? window.event : e;
				if(ev.keyCode==13) {
					$(".sz_dl_right_for_r").click();
				}
			};
		}
};

